<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <div class="title">黑马面面</div>
        <div class="line"></div>
        <div class="sub-title">用户登录</div>
      </div>
      <!-- elementUI表单 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <!-- 手机号 -->
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.password"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-row :gutter="10">
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                prefix-icon="el-icon-key"
                v-model="ruleForm.code"
                placeholder="请输入验证码"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <img
              style="width:100%;height:40px;"
              :src="loginCaptchaUrl"
              alt=""
              ref="RefCaptchaUrl"
              @click="$refs.RefCaptchaUrl.src = loginCaptchaUrl"
            />
          </el-col>
        </el-row>
        <!-- 协议 -->
        <el-form-item prop="checked">
          <el-checkbox v-model="ruleForm.checked"></el-checkbox>&nbsp;
          我已阅读并同意<el-link
            type="primary"
            href="www.baidu.com"
            target="_blank"
            >用户协议</el-link
          >
          和
          <el-link href="www.jd.com" type="primary" target="_blank"
            >隐私条款</el-link
          >
        </el-form-item>
        <!-- 登录 -->
        <el-form-item>
          <el-button
            type="primary"
            style="width:100%"
            @click="sbumitlogin('ruleForm')"
            ref="Reflogin"
            >登录</el-button
          >
        </el-form-item>
        <!-- 注册 -->
        <el-form-item>
          <el-button
            type="primary"
            style="width:100%"
            @click="$refs.Refregister.dialogVisible = true"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/avatar.png" alt="" />
    </div>
    <register ref="Refregister"></register>
  </div>
</template>
<script>
// 导入子组件
import register from '@/views/login/register.vue'
// 导入token.js
import { saveToKen } from '@/router/token'
export default {
  name: 'login',
  components: {
    register
  },
  data () {
    return {
      loginCaptchaUrl: process.env.VUE_APP_BASEURL + '/captcha?type=login', // 验证码的地址
      ruleForm: {
        phone: '',
        password: '',
        code: '',
        checked: true
      },
      rules: {
        phone: [
          {
            required: true,
            validator: (rule, value, callback) => {
              // rule：规则对象，一般不用它
              // value：就是输入的值
              // callback：决定校验成功还是失败，如果失败 callback(new Error('错误提示'))，成功 callback()，无论成功还是失败都要调用
              if (!value) {
                return callback(new Error('手机号不能为空'))
              }

              const reg = /^1[3|4|5|7|8]\d{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不合法'))
              }

              // 一定要调用callback，否则登录按钮就不起效
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          {
            min: 5,
            max: 16,
            message: '请输入6-11位密码',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          {
            min: 4,
            max: 4,
            message: '验证码长度必须是4位',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  mounted () {
    console.log(process.env.VUE_APP_BASEURL)
  },
  methods: {
    sbumitlogin (data) {
      this.$refs[data].validate(async valid => {
        // 全局校验
        if (!valid) return
        // 发请求
        const res = await this.$axios.post('/login', this[data])
        console.log(res)
        if (res.code === 200) {
          this.$message.success('登录成功!')
          // 保存token
          saveToKen(res.data.token)
          this.$router.push('/layout/welcome')
        } else {
          this.$message.error('登录失败')
          this.$refs.RefCaptchaUrl.src = this.loginCaptchaUrl
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
      .login-form {
        margin-top: 29px;
      }
    }
  }
}
</style>
